﻿using CaterModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;


namespace CaterDal
{
    public partial class TableInfoDal
    {
        public int Insert(TableInfo ti)
        {
            string sql = "insert into TableInfo(TTitle,THallId,TIsFree,TIsDelete) values (@TTitle,@THallId,@TIsFree,@TIsDelete)";
            SQLiteParameter[] ps = {
                new SQLiteParameter("@TTitle",ti.TTitle),
                new SQLiteParameter("@THallId",ti.THallId),
                new SQLiteParameter("@TIsFree",ti.TIsFree),
                new SQLiteParameter("@TIsDelete",ti.TIsDelete)
            };
            return SqliteHelper.ExecuteNonQuery(sql, ps);
        }
        public int Delete(int id)
        {
            string sql = "update TableInfo set tisDelete=1  where tid=@id";
            SQLiteParameter p = new SQLiteParameter("@id", id);
            return SqliteHelper.ExecuteNonQuery(sql, p);
        }
        public int Update(TableInfo ti)
        {
            string sql = "update  TableInfo set TTitle=@TTitle,THallId=@THallId,TIsFree=@TIsFree where tid=@id";
            SQLiteParameter[] ps ={
                new SQLiteParameter("@TTitle",ti.TTitle),
                new SQLiteParameter("@THallId",ti.THallId),
                new SQLiteParameter("@TIsFree",ti.TIsFree),
                new SQLiteParameter("@id",ti.TId)
            };
            return SqliteHelper.ExecuteNonQuery(sql, ps);
        }

        public List<TableInfo> GetList(Dictionary<string,string> tic)
        {
            string sql = "select ti.*,hi.htitle  from TableInfo ti,HallInfo hi where ti.THallId=hi.hid and ti.tisDelete=0 and hi.hisdelete=0";

            List<SQLiteParameter> listP = new List<SQLiteParameter>();
            //接收筛选条件
            if(tic.Count>0)
            {
                foreach (var pair in tic)
                {
                    sql += " and ti."+pair.Key+" like @"+pair.Key;
                    listP.Add(new SQLiteParameter("@" + pair.Key,  "%"+pair.Value+"%"));
                }
            }

            DataTable dt = SqliteHelper.GetDataTable(sql,listP.ToArray());
            List<TableInfo> list = new List<TableInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                TableInfo ti = new TableInfo();
                ti.TId =Convert.ToInt32(dr["TId"].ToString());
                ti.TTitle = dr["TTitle"].ToString();
                ti.HTitle= dr["HTitle"].ToString();
                ti.THallId = Convert.ToInt32(dr["THallId"].ToString()); 
                ti.TIsFree = Convert.ToBoolean(dr["TIsFree"].ToString()); 

                list.Add(ti); 
            }

            return list;
        }
    }
}
